<template>
  <el-dialog title="批量策略管理" :visible="isShow" width="630px" @close="closeFn">
    <el-form ref="setForm" :rules="rules" :model="setForm">
      <el-form-item prop="policyId" label="选择策略：" label-width="150px">
      <el-select
          style="width: 85%"
          v-model="setForm.policyId"
          placeholder="请选择"
        >
          <el-option
            v-for="item in policyList"
            :key="item.policyId"
            :label="item.policyName"
            :value="item.policyId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-row type="flex" justify="center">
        <el-col :span="8">
          <el-button
            class="btn cancel"
            @click="closeFn"
            style="margin-right: 20px"
            >取消</el-button
          >
          <el-button class="btn confirm" type="primary" @click="submit"
            >确认</el-button
          >
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { getPolicyApi, applyPolicyApi } from "@/api/vm";
export default {
  props: {
    isShow: Boolean,
    selectedCodes:Array
  },
  data() {
    return {
      setForm: {
        innerCodeList:[],
        policyId: '',
      },
      policyList:[],
      rules: {
        policyId: [
          { required: true, message: "请选择策略", trigger: "blur" },
        ],
      },
    };
  },
  created() { 
    this.getPolicy();
  },
  methods: {
    async getPolicy() {
      const res = await getPolicyApi();
      this.policyList = res
    },
    closeFn() {
      this.$refs.setForm.resetFields();
      this.$emit("update:isShow", false);
    },
    // 提交
    async submit() {
      this.setForm.innerCodeList = this.selectedCodes
      await applyPolicyApi(this.setForm);
      this.$message.success('批量设置策略成功')
      this.closeFn()
    },
  },
};
</script>

<style lang="scss" scoped>
.btn {
  width: 80px;
  height: 36px;
  border: none;
  padding: 0;
  line-height: 36px;
}
.cancel {
  background-color: #fbf4f0;
  color: #7e7570;
}
.confirm {
  background-color: #ff6d29;
  color: #fff;
}
</style>
